/*-------------------------------------------------------------------------------
	JQuery Modal Window (for symfony backend)
	By Giulio Andreini
	
	Parametri:
	- width: larghezza della finestra modale
	- animations: animazione della comparsa della modale
	- submitButtonId: id del pulsante di submit
	- cancelButtonId: id del pulsante cancel
	- larghezza che si vuole impostare ai campi di input: inputsWidth
		
	Creare come:
	$("#item").nfModal({width: 600, animations: false, submitButtonId: "id_del_pulsante_submit", cancelButtonId: "id_del_pulsante_cancel", inputsWidth: 100});
-------------------------------------------------------------------------------*/
$.fn.nfModal = function(options){

	/* Setup the options */  
	var defaults = {  
		width: 300,
		animations: true,
		submitButtonId: "nf_modal_form_submit",
		cancelButtonId: "nf_modal_form_cancel",
		inputsWidth: -1
	};  
	var options = $.extend(defaults, options); 
	
	// Loop su gli elementi invocati
	$(this).each(function(){
		// Assegno elemento this
		var $this = $(this);
		var $overlay;
		
		// Nascondo tutti gli elementi di input con attributo hidden
		$(this).find("input").each(function(){
			if( $(this).attr("type") == "hidden") $(this).hide();
		});
		
		// Creazione del bg scuro - overlay
		createOverlay = function() {
		    
		    // Creo un elemento di bg nero per lo sfondo
			$this.before("<div id='modalBg'></b>");
			
			var ie6 = !window.XMLHttpRequest;
			$overlay = $("#modalBg");
			
			var overlayOpacity= 0.7;		// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			var overlayFadeDuration= 1;		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
		
			// Overlay;
			compatibleOverlay = ie6 || ($overlay.currentStyle && ($overlay.currentStyle.position != "fixed"));
			// if (compatibleOverlay) $overlay.style.position = "absolute";
			if (compatibleOverlay) $overlay.css({"position":"absolute"});
			$overlay.css("opacity", overlayOpacity);
			// $overlay.css({"display":"none", "margin":"0", "padding":"0", "position":"absolute", "background-color":"#000000", "width":"100%", "height":$(document).height()+"px","top":"0","left":"0", "z-index":"1001"});
			$overlay.css({"display":"none", "margin":"0", "padding":"0", "position":"absolute", "background-color":"#000000", "width":"100%", "height":$(document).height()+"px","top":"0","left":"0"});
			$overlay.fadeIn(overlayFadeDuration, function(){showModal();}); // Chiamata alla funzione shoeModal alla fine del fade
			
			// Click event sull overlay - nasconde tutto
			$overlay.click(function () { 
		    	removeModalElements();
		    });
		} // createOverlay
		
		    
		// Mostra la pop up
		showModal = function() {
			// Set the width
			$this.css({"width":defaults.width + "px"});
			
			// Se l'h3  vuoto lo rimuovo
			if($this.find("h3:first").html() == "") $this.find("h3:first").remove();
			
			// Page height & width
			var pageHeight = $(window).height();
			var pageWidth = $(window).width();
			
			// Set vertical positioning
			$this.vCenter();     
			
			// Set orizontal positioning
			$this.css({"margin-left": "-" + $this.width()/2 + "px"});
			
			// Z-indexing
			// $this.css({"z-index":"1002"});
			
			// Animazione comparsa
			 if(defaults.animations == true) 
			 	$this.animate( { "top": "-=20px" }, { queue:false, duration:300 } ).animate( { "opacity": "toggle"}, 200 );
         	 else
         	 	$this.animate( { "opacity": "toggle"}, 50 );	
         	
         	// Assegno il focus al primo campo di input
        	$this.find("input:first").focus();
        	
        	// Assegno la funzione di chiusura al pulsante cancel
        	$this.find("input#"+defaults.cancelButtonId).click(function () {
        		removeModalElements();
        		return false;
        	});
        	
        	
        } // showModal
		
		// Resize input fields
		resizeInputs = function() {
			if(defaults.inputsWidth != -1)
			{
				$this.find("input:not(#"+defaults.cancelButtonId+", #"+defaults.submitButtonId+")").css({"width": defaults.inputsWidth + "px"});
				$this.find("textarea").css({"width": defaults.inputsWidth + "px"});
				$this.find("select").css({"width": defaults.inputsWidth + "px"});
			}
		}
		
		// Mostra la pop up
		startModal = function() {
		    createOverlay();
			resizeInputs();
		}
		
		// Gestione dello scrolling della pagina
		// Si riposiziona verticalmente l'oggetto
		$(window).scroll(function () { 
			$this.vCenter();
	    });

		
		// Remove degli elementi dopo un bel fade out
		removeModalElements = function() {
			// Animazione scomparsa
			if(defaults.animations == true)
			{
				$this.animate( { "top": "-=20px" }, { queue:false, duration:300 } )
	         		.animate( { "opacity": "toggle"}, 200, function() {
	   				 $this.remove();
	   				 $overlay.fadeOut(200, function() {$overlay.remove();});
				});
			}else
			{
				$this.animate( { "opacity": "toggle"}, 50, function() {
	   				 $this.remove();
	   				 $overlay.fadeOut(200, function() {$overlay.remove();});
				});
			}
		}


		// Chiamata iniziale
		startModal();
	});
};


